<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE topic
  PUBLIC "-//OASIS//DTD DITA Composite//EN" "ditabase.dtd">
<topic id="topic1">
    <title id="be20941">BEGIN</title>
    <body>
        <p id="sql_command_desc">Starts a transaction block.</p>
        <section id="section2">
            <title>Synopsis</title>
            <codeblock id="sql_command_synopsis">BEGIN [WORK | TRANSACTION] [<varname>transaction_mode</varname>]</codeblock>
            <p>where <varname>transaction_mode</varname> is:</p>
            <codeblock>   ISOLATION LEVEL {READ UNCOMMITTED | READ COMMITTED | REPEATABLE READ | SERIALIZABLE}
   READ WRITE | READ ONLY
   [ NOT ] DEFERRABLE</codeblock>
        </section>
        <section id="section3">
            <title>Description</title>
            <p><codeph>BEGIN</codeph> initiates a transaction block, that is, all statements after a
                    <codeph>BEGIN</codeph> command will be run in a single transaction until an
                explicit <codeph>COMMIT</codeph> or <codeph>ROLLBACK</codeph> is given. By default
                (without <codeph>BEGIN</codeph>), Greenplum Database runs transactions in
                autocommit mode, that is, each statement is run in its own transaction and a
                commit is implicitly performed at the end of the statement (if execution was
                successful, otherwise a rollback is done). </p>
            <p>Statements are run more quickly in a transaction block, because transaction
                start/commit requires significant CPU and disk activity. Execution of multiple
                statements inside a transaction is also useful to ensure consistency when making
                several related changes: other sessions will be unable to see the intermediate
                states wherein not all the related updates have been done. </p>
            <p>If the isolation level, read/write mode, or deferrable mode is specified, the new
                transaction has those characteristics, as if <codeph><xref
                        href="./SET_TRANSACTION.xml#topic1" type="topic" format="dita"/></codeph>
                was run. </p>
        </section>
        <section id="section4">
            <title>Parameters</title>
            <parml>
                <plentry>
                    <pt>WORK</pt>
                    <pt>TRANSACTION</pt>
                    <pd>Optional key words. They have no effect.</pd>
                </plentry>
                <plentry>
                    <pt>SERIALIZABLE</pt>
                    <pt>READ COMMITTED</pt>
                    <pt>READ UNCOMMITTED</pt>
                    <pd>The SQL standard defines four transaction isolation levels: <codeph>READ
                            UNCOMMITTED</codeph>, <codeph>READ COMMITTED</codeph>,
                            <codeph>REPEATABLE READ</codeph>, and
                        <codeph>SERIALIZABLE</codeph>.</pd>
                    <pd><codeph>READ UNCOMMITTED</codeph> allows transactions to see changes made by
                        uncomitted concurrent transactions. This is not possible in Greenplum
                        Database, so <codeph>READ UNCOMMITTED</codeph> is treated the same as
                            <codeph>READ COMMITTED</codeph>.</pd>
                    <pd><codeph>READ COMMITTED</codeph>, the default isolation level in Greenplum
                        Database, guarantees that a statement can only see rows committed before it
                        began. The same statement run twice in a transaction can produce
                        different results if another concurrent transaction commits after the
                        statement is run the first time. </pd>
                    <pd>The <codeph>REPEATABLE READ</codeph> isolation level guarantees that a
                        transaction can only see rows committed before it began. <codeph>REPEATABLE
                            READ</codeph> is the strictest transaction isolation level Greenplum
                        Database supports. Applications that use the <codeph>REPEATABLE
                            READ</codeph> isolation level must be prepared to retry transactions due
                        to serialization failures.</pd>
                    <pd>The <codeph>SERIALIZABLE</codeph> transaction isolation level guarantees
                        that running multiple concurrent transactions produces the same effects as
                        running the same transactions one at a time. If you specify
                            <codeph>SERIALIZABLE</codeph>, Greenplum Database falls back to
                            <codeph>REPEATABLE READ</codeph>.</pd>
                    <pd>Specifying <codeph>DEFERRABLE</codeph> has no effect in Greenplum Database,
                        but the syntax is supported for compatibility with PostgreSQL. A transaction
                        can only be deferred if it is <codeph>READ ONLY</codeph> and
                            <codeph>SERIALIZABLE</codeph>, and Greenplum Database does not support
                            <codeph>SERIALIAZABLE</codeph> transactions. </pd>
                </plentry>
            </parml>
        </section>
        <section id="section5">
            <title>Notes</title>
            <p><codeph><xref href="./START_TRANSACTION.xml#topic1" type="topic" format="dita"
                    /></codeph> has the same functionality as <codeph>BEGIN</codeph>. </p>
            <p>Use <codeph><xref href="./COMMIT.xml#topic1" type="topic" format="dita"/></codeph> or
                        <codeph><xref href="./ROLLBACK.xml#topic1" type="topic" format="dita"
                    /></codeph> to terminate a transaction block. </p>
            <p>Issuing <codeph>BEGIN</codeph> when already inside a transaction block will provoke a
                warning message. The state of the transaction is not affected. To nest transactions
                within a transaction block, use savepoints (see <codeph>SAVEPOINT</codeph>).</p>
        </section>
        <section id="section6">
            <title>Examples</title>
            <p>To begin a transaction block: </p>
            <codeblock>BEGIN;</codeblock>
            <p>To begin a transaction block with the repeatable read isolation level:</p>
            <codeblock>BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;</codeblock>
        </section>
        <section id="section7">
            <title>Compatibility</title>
            <p><codeph>BEGIN</codeph> is a Greenplum Database language extension. It is equivalent
                to the SQL-standard command <codeph><xref href="./START_TRANSACTION.xml#topic1"
                        type="topic" format="dita"/></codeph>.</p>
            <p><codeph>DEFERRABLE</codeph>
                <varname>transaction_mode</varname> is a Greenplum Database language extension.</p>
            <p>Incidentally, the <codeph>BEGIN</codeph> key word is used for a different purpose in
                embedded SQL. You are advised to be careful about the transaction semantics when
                porting database applications.</p>
        </section>
        <section id="section8">
            <title>See Also</title>
            <p><codeph><xref href="./COMMIT.xml#topic1" type="topic" format="dita"/></codeph>,
                        <codeph><xref href="./ROLLBACK.xml#topic1" type="topic" format="dita"
                    /></codeph>, <codeph><xref href="./START_TRANSACTION.xml#topic1" type="topic"
                        format="dita"/></codeph>, <codeph><xref href="./SAVEPOINT.xml#topic1"
                        type="topic" format="dita"/></codeph></p>
        </section>
    </body>
</topic>
